/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.messaging.inbox;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import net.miginfocom.swing.MigLayout;
import nl.cloudfarming.client.messaging.Message;
import nl.cloudfarming.client.messaging.MessageModule;
import org.openide.util.NbBundle;

/**
 * JPanel which contains InboxRows. It has a vertical scrollbar.
 * @author Merijn Zengers
 */
public class MessageListPanel extends JScrollPane implements FocusListener, MouseListener {

    private List<InboxRow> inBoxRows;
    private String INBOX_TITLE = NbBundle.getMessage(MessageModule.class, "messaging.message_inbox.inbox_label");
    private JPanel rowPanel;
    private JLabel inboxColumnLabel;
    
    private JViewport headerViewPort;
    public static final int MINIMUM_WIDTH = 200;
    public static final int MINIMUM_HEIGHT = 300;
    
    private static final Color BACKGROUND_COLOR = Color.WHITE;
    private static final Color HEADER_COLOR = Color.DARK_GRAY;
    
    private static final String PROPERTY_SELECTED = "selected";
    
    private final PropertyChangeSupport propertyChangeSupport;

    /** Creates new MessageListPanel */
    public MessageListPanel() {
        propertyChangeSupport = new PropertyChangeSupport(this);
        initComponents();
        inBoxRows = new ArrayList<InboxRow>();
    }

    private void initComponents() {
        rowPanel = new JPanel();
        rowPanel.setName("rowPanel");
        rowPanel.setLayout(new MigLayout("wrap 1, insets 0"));
        rowPanel.setBackground(BACKGROUND_COLOR);
        
        
        
        this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        
        headerViewPort = new JViewport();
        
        inboxColumnLabel = new JLabel(INBOX_TITLE);
        
        inboxColumnLabel.setBackground(HEADER_COLOR);
        headerViewPort.setView(inboxColumnLabel);
        
        this.setColumnHeader(headerViewPort);

        Dimension dimension = new Dimension(MINIMUM_WIDTH, MINIMUM_HEIGHT);
        this.setPreferredSize(dimension);
        
        this.setViewportView(rowPanel);
        
    }


    /**
     * Adds a message into the inbox
     * @param message the message to be inserted
     */
    public synchronized void addMessage(Message message) {
        InboxRow row = new InboxRow(message);
        inBoxRows.add(row);
        row.addFocusListener(this);
        row.addMouseListener(this);
        rowPanel.add(row);
    }

    /**
     * Get the selected message in the inbox
     * @return the selected InboxRow
     */
    public synchronized InboxRow getSelectedInboxRow() {
        for (InboxRow inboxRow : inBoxRows) {
            if (inboxRow.isSelected()) {
                return inboxRow;
            }
        }
        return null;
    }

    public void addMessagePropertyChangeListener(PropertyChangeListener pcl) {
        propertyChangeSupport.addPropertyChangeListener(PROPERTY_SELECTED, pcl);
    }

    
    public void removeMessagePropertyChangeListener(PropertyChangeListener pcl) {
        super.removePropertyChangeListener(pcl);
        propertyChangeSupport.removePropertyChangeListener(PROPERTY_SELECTED, pcl);
    }

    /**
     * Is fired when a InboxRow gets focus
     * Fires a MessageSelectedEvent 
     * @param e 
     */
    @Override
    public void focusGained(FocusEvent e) {
        for (PropertyChangeListener listener : propertyChangeSupport.getPropertyChangeListeners(PROPERTY_SELECTED)) {
            listener.propertyChange(new PropertyChangeEvent(e.getSource(), PROPERTY_SELECTED, "" , ""));
        }
    }

    @Override
    public void focusLost(FocusEvent e) {
    }

    /**
     * When a message is dubbleclicked open popup
     * @param e 
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        //double click
        if (e.getClickCount() == 2) {
            // Goto messagePopUp
            MessageDisplayFrame messagePopUp = new MessageDisplayFrame(getSelectedInboxRow().getMessage());
            messagePopUp.setVisible(true);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
